function ajax({url,type,data,success,error,dataType}){
    if(!url){
        alert("必须传入地址");
        return false;
    }
    type = type || 'get';
    data = data || {};
    dataType = dataType || 'text';
    
    let xhr = new XMLHttpRequest();
    
    //处理data
    let reqParams = [];
    if(data){
        for(let name in data){
            reqParams.push(name+"="+data[name])
        }
    }
    data = reqParams.join('&');
    switch (type) {
        case 'get':
            xhr.open(type,encodeURI(url+'?'+data),true);
            xhr.send();
            break;
        case 'post':
            xhr.open(type,encodeURI(url),true);
            xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");
            xhr.send(encodeURI(data));
            break;
        default:
            alert('暂不支持此请求方式');
            return;
            break;
    }

    xhr.onreadystatechange = function(){
        if(xhr.readyState == 4){
            if(xhr.status == 200){
                let res;
                switch (dataType) {
                    case 'xml':
                        res = xhr.responseXML;
                        break;
                    default:
                        res = xhr.responseText;
                        break;
                }
                success && success(res);
            }else{
                error && error('错了');
            }
        }
    }
}